class environment (window:Interface.window1) =
object (self)
  val gui = window
  val image = new Image.imData
  val mutable color = Array.make 50 (0,0,0)
  val mutable height = Array.make 50 (0)
  val mutable heightlist = []
  val mutable n = 0
    
  method drawImg = gui#image4#set_file (image#filename^".tmp")
  (*this one bug*)    
  method prompt_colors =
    let parse =
      let (w,h)= image#dims in
      for i = 0 to w-1 do
	for j = 0 to h-1 do
	  let (r,g,b) = Sdlvideo.get_pixel_color image#original i j in
	  if (MapTools.existpix (r,g,b) (Array.to_list color)) then ()
	  else begin
	    n <- n+1;
	    color.(n) <- (r,g,b);
	  end
	done
      done
    in
    let table = GPack.table 
      ~rows:n (* Nombre de rangées. *)
      ~columns:2 (* Nombre de colonnes. *)
      ~row_spacings:2 (* Espacement des éléments d'une même rangée. *)
      ~col_spacings:2 (* Espacement des éléments d'une même colonne. *)
      ~packing:gui#vbox3#add () in
    for i = 0 to n do
      let button = GEdit.spin_button
	~show:true
        ~width:80
	~height:30
	~packing:(table#attach ~left:1 ~top:i) () in
      ()
    done
	
    
  method analyse = function () ->
    if image#isInit () then
      begin
	(*MapTools.median_filter image#original image#current;*)
	MapTools.traitement image#original image#current;
	if (image#grey) then MapTools.image2grey image#current image#current;
	if (image#grid) then MapTools.quadrille image#current image#current (10*int_of_float(gui#hscale1#adjustment#value));
	Sdlvideo.save_BMP image#current (image#filename^".tmp");
	self#drawImg;
	gui#status#set_text "Done"
      end
    else
      gui#status#set_text "Please select a file"

    method makeObj = 
    if image#isInit () then
      begin if (color.(1) = (0,0,0)) then
	  begin
	    self#prompt_colors;
	    gui#status#set_text "Enter height values"
	  end
	else
	  begin
	    MapTools.objMake image#original (10*int_of_float(gui#hscale1#adjustment#value)) [];
	    gui#status#set_text ".obj generated"
	  end
      end
    else
      gui#status#set_text "Please select a file"
	
    method first_im = gui#image1#set_file image#filename

  method bind_callbacks =
    ignore (gui#imagechooser#connect#update_preview ~callback:(function () -> (image#load (gui#imagechooser#filename); self#first_im)));
    ignore (gui#button1#connect#clicked ~callback:self#analyse);
    ignore (gui#hscale1#connect#value_changed ~callback:self#analyse);
    ignore (gui#togglebutton1#connect#clicked ~callback:image#toggle_grid);
    ignore (gui#togglebutton1#connect#clicked ~callback:self#analyse);
    ignore (gui#togglebutton1#connect#clicked ~callback:(function () -> self#makeObj));
    ignore (gui#togglebutton2#connect#clicked ~callback:image#toggle_grey);
    ignore (gui#togglebutton2#connect#clicked ~callback:self#analyse);
    ignore (gui#button2#connect#clicked ~callback:(function () -> (if image#isInit () then MapTools.savepixelcolor image#current else ())));    

  method init_gui = 
    gui#toplevel#show ();
    gui#check_widgets ();
    ignore (self#bind_callbacks);
    GMain.Main.main ()

end
